﻿/***************************************************************
 * 开发人员：网魂小兵@http://www.mozlite.com
 * 
 * 通用辅助方法类。
 * 
 * 最后修改：2011-10-29
 * ************************************************************/
namespace Mozlite
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    using Mozlite.Common;
    using System.Reflection;
    using Mozlite.Common.Configuration;
    using System.Threading;
    using System.Globalization;

    /// <summary>
    /// 通用辅助方法类。
    /// </summary>
    public class Globals : CommonHelper
    {
        private Globals() { }

        /// <summary>
        /// 验证当前请求的URL是否为合理的URL地址。
        /// </summary>
        public static void ValidUrl() {
            if (!AppContext.Current.IsWebRequest)
                return;
            var siteDomain = AppContext.Current.SiteDomain;
            if (siteDomain == null)
            {
                AppContext.Current.Context.Response.Redirect(SiteSettings.Default.SiteUrl, true);
            }
        }

        #region endlogs
        /// <summary>
        /// 记录应用程序关闭的原因。
        /// </summary>
        public static void RecordEndReason()
        {
            HttpRuntime runtime = (HttpRuntime)typeof(System.Web.HttpRuntime).InvokeMember("_theRuntime", BindingFlags.NonPublic | BindingFlags.Static | BindingFlags.GetField, null, null, null);
            if (runtime == null)
                return;
            string shutDownMessage = (string)runtime.GetType().InvokeMember("_shutDownMessage", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
            string shutDownStack = (string)runtime.GetType().InvokeMember("_shutDownStack", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField, null, runtime, null);
            Logs.Info(String.Format("应用程序结束：\r\n_shutDownMessage={0}\r\n\r\n_shutDownStack={1}", shutDownMessage, shutDownStack), "Web 事件", EventID.Events, "ASP.NET " + DotNetVersion.ToString(), SiteSettings.Default.SettingsID);
        }
        #endregion
    }
}
